<template>
  <view>
    <!-- 引用搜索框组件 -->
    <my-search @click="mySearchClickHandel"></my-search>
    
    <view class="scroll-view-container">
      <!-- 左侧滚动区域 -->
      <scroll-view scroll-y="true" class="left-scroll-view" :style="{height:wh + 'px'}">
        <view :class="['left-scroll-view-item', activeIndex === index? 'active':'']" v-for="(item,index) in cateList" :key="index" @click="activeChange(index)">{{item.cat_name}}</view>
      </scroll-view>
      <!-- 右侧滚动区域 -->
      <scroll-view scroll-y="true" class="right-scroll-view" :style="{height:wh + 'px'}" :scroll-top="scrollTop">
      <!-- 每个二级分类盒子（二级分类标题包三级分类内容） -->
        <view class="cate-lv2" v-for="(item,index) in cateLevel2" :key="index">
          <!-- 二级分类标题 -->
          <view class="cate-lv2-title">{{item.cat_name}}</view>
            <!-- 三级分类内容盒子 -->
          <view class="cate-lv2-list">
            <!-- 三级分类内容每一项 -->
            <view class="cate-lv2-item" @click="goGoodsList(item3)" v-for="(item3,index3) in item.children" :key="index3">
              <image :src="item3.cat_icon"></image>
              <text>{{item3.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
    import tabbarBadge from '../../mixins/tabbar-badge';
  export default {
        mixins:[tabbarBadge],
    data() {
      return {
        wh:0,
        cateList:[],
        activeIndex:0,
        cateLevel2:[],
        scrollTop:0
        
      };
    },
    onLoad(){
      //获取屏幕高度
      const sysInfo = uni.getSystemInfoSync()
      //要减去上方搜索框高度
      this.wh = sysInfo.windowHeight - 50
      
      this.getCateList()
    },
    methods:{
     async getCateList(){
        const res = await uni.$http.get('/api/public/v1/categories')
        console.log(res)
        if(res.data.meta.status !== 200) return uni.$showMsg
        this.cateList = res.data.message
        //当获取分类列表时默认先获取第一个二级分类，不然二级分类为空
        this.cateLevel2 = res.data.message[0].children
      },
      activeChange(index){
        //点击事件，当点击的当前索引赋值给activeIndex
        //当activeIndex 与当前索引值相同时，加上active类名高亮
        this.activeIndex = index
        //根据点击到的索引值，动态获取二级分类列表
        this.cateLevel2 = this.cateList[index].children
        //让srollTop在0和1之间切换
        this.scrollTop = this.scrollTop === 0? 1 :0
      },
      goGoodsList(item3){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?goods_id=' + item3.cat_id
        })
      },
      mySearchClickHandel(){
        console.log(111)
      }
    }
  }
</script>

<style lang="scss">
.scroll-view-container {
  display: flex;
  .left-scroll-view {
    width: 120px;
    .left-scroll-view-item{
      line-height: 60px;
      background-color: #f7f7f7;
      text-align: center;
      font-size: 12px;      &.active {
        background-color: #ffffff;
        position: relative;
          &::before{
            content: '';
            display: block;
            width: 3px;
            height: 30px;
            background-color: #c00000;
            position: absolute;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
          }
      }
    }
  }
  .right-scroll-view{
    .cate-lv2{
      .cate-lv2-title{
        font-size: 12px;
        font-weight: bold;
        text-align: center;
      }
     .cate-lv2-list{
       display: flex;
       flex-wrap: wrap;
       .cate-lv2-item{
         display: flex;
         flex-wrap: wrap;
         flex-direction: column;
         justify-content: center;
         align-items: center;
         width: 33.33%;
         margin-bottom: 10px;
         image{
           width: 60px;
           height: 60px;
         }
         text{
           font-size: 12px;
         }
       }
     }
    }
  }
  
}
</style>
